package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import modelo.Persona;
import util.ConexionBD;
import excepcion.DAOExcepcion;

public class PersonaDAO extends BaseDAO {

	public Persona Obtener(String correo, String password) throws DAOExcepcion {
		System.out
				.println("PersonaDAO: Obtener(String correo, String password)");
		String query = "SELECT * FROM Persona WHERE correo=? and contrasena=?";
		Connection con = null;
		PreparedStatement stmt = null;
		Persona persona = null;
		try {

			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);

			stmt.setString(1, correo);
			stmt.setString(2, password);
			ResultSet rs = stmt.executeQuery();

			if (rs.next()) {
				persona = new Persona();
				persona.setIdPersona(rs.getInt(1));
				persona.setIdTipo(rs.getInt(2));
				persona.setNombres(rs.getString(3));
				persona.setPaterno(rs.getString(4));
				persona.setMaterno(rs.getString(5));
				persona.setSexo(rs.getString(6).charAt(0));
				persona.setDocumentoTipo(rs.getInt(7));
				persona.setDocumentoNumero(rs.getString(8));
				persona.setCorreo(rs.getString(9));
				persona.setClave(rs.getString(10));
				persona.setFechaNacimiento(rs.getDate(11));
				persona.setCelular(rs.getString(12));
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return persona;
	}
}
